package com.biqee.service.crm.impl;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.biqee.dao.BqCustomerDAO;
import com.biqee.dao.TbTradeDAO;
import com.biqee.model.db.BqCustomer;
import com.biqee.model.db.TbTrade;
import com.biqee.service.crm.CustomerService;
import com.biqee.util.DateUtil;
import com.biqee.util.HtmlParseUtil;
import com.taobao.api.domain.Trade;

public class CustomerServiceImpl implements CustomerService {
	private static final Logger logger = Logger
			.getLogger(CustomerServiceImpl.class);

	TbTradeDAO tbTradeDAO;

	BqCustomerDAO bqCustomerDAO;

	@Override
	public void importDataMap(Long owner, List<Map<String, String>> dataList) {

		if (dataList != null) {
			for (Map data : dataList) {
				try {
					TbTrade tbTrade = new TbTrade();
					BeanUtils.populate(tbTrade, data);
					if (tbTrade.getTid() != null) {
						TbTrade dbData = tbTradeDAO.selectByTid(tbTrade
								.getTid());
						if (dbData == null) {
							tbTradeDAO.insert(tbTrade);
							importCustomerByTrade(owner, tbTrade);
						}
					}
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
	}

	public void importCustomerByTrade(Long owner, TbTrade tbTrade) {
		if (tbTrade.getBuyerNick() != null) {
			BqCustomer customer = getCustomerByNick(owner,
					tbTrade.getBuyerNick());
			if (customer == null) {
				BqCustomer newCustomer = buildNewCustomer();
				newCustomer.setOwner(owner);
				newCustomer.setMobile(HtmlParseUtil.getNumber(tbTrade.getReceiverMobile()));
				newCustomer.setReceiveEmail(tbTrade.getBuyerAlipayNo());
				newCustomer.setReceiverAddress(tbTrade.getReceiverAddress());
				newCustomer.setReceiverCity(tbTrade.getReceiverCity());
				newCustomer.setReceiverDistrict(tbTrade.getReceiverDistrict());
				newCustomer.setReceiverName(tbTrade.getReceiverName());
				newCustomer.setNick(tbTrade.getBuyerNick());
				newCustomer.setReceiverState(tbTrade.getReceiverState());
				newCustomer.setReceiverZip(tbTrade.getReceiverZip());
				newCustomer.setTotalReceivedPayment(tbTrade
						.getReceivedPayment());
				try {
					newCustomer.setRecentDealTime(DateUtil
							.parseDateTime(tbTrade.getEndTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				bqCustomerDAO.insert(newCustomer);
			}

		}
	}

	private BqCustomer buildNewCustomer() {
		BqCustomer newCustomer = new BqCustomer();
		newCustomer.setOwnerNick("-1");
		newCustomer.setGmtCreate(new Date());
		newCustomer.setGmtModified(new Date());
		newCustomer.setGroupTag(0L);
		return newCustomer;
	}

	public BqCustomer getCustomerByNick(Long owner, String nick) {
		BqCustomer bqCustomer = bqCustomerDAO.selectByNick(owner, nick);
		return bqCustomer;
	}

	public void setTbTradeDAO(TbTradeDAO tbTradeDAO) {
		this.tbTradeDAO = tbTradeDAO;
	}

	public void setBqCustomerDAO(BqCustomerDAO bqCustomerDAO) {
		this.bqCustomerDAO = bqCustomerDAO;
	}

	@Override
	public void importTrade(Long owner, List<Trade> tradeList) {
		if(tradeList!=null){
			for (Trade trade : tradeList) {
				TbTrade tbTrade = build(trade);
				tbTrade.setOwner(owner);
				synTrade(tbTrade);
			}
		}
		
	}
	
	private void synTrade(TbTrade tbTrade ){
		TbTrade dbData = tbTradeDAO.selectByTid(tbTrade
				.getTid());
		if (dbData == null) {
			tbTradeDAO.insert(tbTrade);
			importCustomerByTrade(tbTrade.getOwner(), tbTrade);
		}else{
			tbTrade.setId(dbData.getId());
			tbTradeDAO.updateByPrimaryKeySelective(tbTrade);
		}
	}
	
	
	public TbTrade build(Trade trade ){
		TbTrade tbTrade = new TbTrade();
		try {
			BeanUtils.copyProperties(tbTrade, trade);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tbTrade;
	}
	
	
}
